/*
Pic Resizer : an easy to use program to resize your pictures
Copyright (C) 2009  Kévin Gomez <geek63@gmail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ProfilDial.h"

ProfilDial::ProfilDial(ProfilesManager *p_manager, QWidget *parent) : QDialog(parent)
{
    //gestionnaire de profiles
    manager = p_manager;
    edit_mode = false;

    setupUI();
    setWindowTitle(tr("Ajouter un nouveau profil"));
}

ProfilDial::ProfilDial(ProfilesManager *p_manager, Profil *profil, QWidget *parent) : QDialog(parent)
{
    //gestionnaire de profiles
    manager = p_manager;
    old_profil = profil;
    edit_mode = true;

    setupUI();
    ok_w->setText("Modifier");
    setWindowTitle(tr("Modifier un profil"));

    fillFields();
}

void ProfilDial::setupUI()
{
    setModal(true);

    //création des widget
    nom_w = new QLineEdit;
    hauteur_w = new QSpinBox;
    largeur_w = new QSpinBox;
    save_proportions_w = new QCheckBox(this);

    ok_w = new QPushButton(tr("Ajouter"), this);
    cancel_w = new QPushButton(tr("Annuler"), this);

    // "config" des widget
    largeur_w->setRange(1, 4000);
    largeur_w->setSuffix(tr(" px"));

    hauteur_w->setRange(1, 4000);
    hauteur_w->setSuffix(tr(" px"));

    save_proportions_w->setChecked(true);

    //construction du layout
    QHBoxLayout *buttons_l = new QHBoxLayout;
    buttons_l->addWidget(ok_w);
    buttons_l->addWidget(cancel_w);

    QFormLayout *form_l = new QFormLayout;
    form_l->addRow(tr("&Nom :"), nom_w);
    form_l->addRow(tr("&Largeur :"), largeur_w);
    form_l->addRow(tr("&Hauteur :"), hauteur_w);
    form_l->addRow(tr("Conserver les &proportions :"), save_proportions_w);
    form_l->addRow(buttons_l);
    setLayout(form_l);

    createActions();
}

void ProfilDial::fillFields()
{
    nom_w->setText(old_profil->name);
    hauteur_w->setValue(old_profil->width);
    largeur_w->setValue(old_profil->height);
    save_proportions_w->setChecked(old_profil->save_proportions);
}

void ProfilDial::createActions()
{
    connect(ok_w, SIGNAL(clicked()), this, SLOT(createProfil()));
    connect(cancel_w, SIGNAL(clicked()), this, SLOT(reject()));
}

void ProfilDial::createProfil()
{
    QString profil_name = nom_w->text();

    //si le nom n'est pas indiqué, on refuse
    if(profil_name.isEmpty())
    {
        QMessageBox::information(this, tr("Erreur !"),
                                 tr("Vous devez spécifier un nom pour le profil."));

        return;
    }

    //si on édite un profil, on supprime l'ancienne version
    if(edit_mode)
    {
        manager->remove(old_profil->name);
    }

    //si le nom est déjà utilisé, on refuse aussi =p
    //moche !
    QList<Profil> list = manager->get();
    for (int i=0; i < list.size(); ++i)
    {
        if(profil_name == list.at(i).name)
        {
            QMessageBox::information(this, tr("Erreur !"),
                                     tr("Ce nom est déjà utilisé pour un autre profil."));

            return;
        }
    }

    Profil profil = manager->add(nom_w->text(), hauteur_w->value(),
                                largeur_w->value(), save_proportions_w->isChecked());

    emit profilAdded(profil);

    accept();
}


void ProfilDial::run()
{
    show();
    exec();
}
